#include <QtGui/QSpinBox>
#include <QtGui/QToolButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include "GoToLineWidget.h"

GoToLineWidget::GoToLineWidget(QWidget *parent) : QWidget(parent) {
	spinBox = new QSpinBox;
	spinBox->setMinimum(1);
	connect(spinBox,SIGNAL(editingFinished()),this,SLOT(go()));
	
	goButton = new QToolButton;
	goButton->setAutoRaise(true);
	goButton->setIcon(QIcon(":/icons/goto_line.png"));
	connect(goButton,SIGNAL(clicked()),this,SLOT(go()));

	cancelButton = new QToolButton;
	cancelButton->setAutoRaise(true);
	cancelButton->setIcon(QIcon(":/icons/close.png"));
	cancelButton->setShortcut(QKeySequence("Esc"));
	connect(cancelButton,SIGNAL(clicked()),this,SLOT(hide()));
	
	QHBoxLayout *mainLayout = new QHBoxLayout;
	mainLayout->addWidget(new QLabel("<b>" + tr("Go to line number") + "</b>"));
	mainLayout->addWidget(spinBox,1);
	mainLayout->addWidget(goButton);
	mainLayout->addWidget(cancelButton);
	mainLayout->setContentsMargins(10,0,5,0);
	setLayout(mainLayout);
}

GoToLineWidget::~GoToLineWidget() {
	delete cancelButton;
	delete spinBox;
	delete goButton;
}

void GoToLineWidget::setMaximum(int maximumLine) {
	spinBox->setMaximum(maximumLine);
}

void GoToLineWidget::go() {
	emit (goToLine(spinBox->value()));
	hide();
}

void GoToLineWidget::focusInEvent(QFocusEvent *) {
	emit (changeMaximum());
}

void GoToLineWidget::showWidget() {
	show();
	spinBox->setFocus();
	spinBox->selectAll();
	emit (changeMaximum());
}
